package com.bill99.codecheck.sonar.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.bill99.codecheck.sonar.orm.entity.RnmItem;
import com.bill99.codecheck.sonar.orm.manager.ElmMng;
import com.bill99.codecheck.sonar.service.ElmDataService;
import com.bill99.codecheck.tools.DateUtil;

/**
 * 
 * @author guoan.ding
 * 处理elm 数据库数据，按照checkout svnpath 要求 及服务器建立文件夹 temppath
 * 
 */
public class ElmDataServiceImpl implements ElmDataService {
	private ElmMng elmMng;
	private String prefixbuildpath;

	//	D:/exceltest/sonar/ant/src/

	public void setElmMng(ElmMng elmMng) {
		this.elmMng = elmMng;
	}

	public void setPrefixbuildpath(String prefixbuildpath) {
		this.prefixbuildpath = prefixbuildpath;
	}

	private static final Pattern pattern = Pattern.compile("(?<=^((U\\s)|(A\\s)|(\\wU)|(A\\+))\\s{2})(.*)");
	private static final Pattern pattern2 = Pattern.compile("(?<=src/main/java/)(.*?)(?=/[a-zA-z0-9]+\\.java)");
	private static final Pattern pattern3 = Pattern.compile("(?<=src/main/java/)(.*?)(?>/[a-zA-z0-9]+\\.java)");

	@Override
	public Map<String, List<RnmItem>> elmDealData() {

		Map<String, List<RnmItem>> SonarMapNew = new HashMap<String, List<RnmItem>>();
		Map<String, List<RnmItem>> rnmItemMaps = elmMng.findRnmItems();
		for (Entry<String, List<RnmItem>> rnmItemMap : rnmItemMaps.entrySet()) {
			String taskemailString = rnmItemMap.getKey();
			List<RnmItem> listcontent = new ArrayList<RnmItem>();
			String sonarjobfileString = DateUtil.getDate();
			String sonarjobnametimeString = DateUtil.gethour();

			StringBuffer arrayBuffer3 = new StringBuffer();
			String sonarjobname = arrayBuffer3.append(taskemailString).append(sonarjobnametimeString).toString();

			StringBuffer arrayBuffer4 = new StringBuffer();
			String buildpath = arrayBuffer4.append(prefixbuildpath).append(sonarjobfileString).append("/").append(sonarjobname).append("/").toString();
			String dealbuildxmlpath = arrayBuffer4.append("build.xml").toString();

			//			System.out.println("key =" + rnmItemMap.getKey());
			/*if (!taskemailString.contains("@99bill.com")) {
				StringBuffer arrayBuffer = new StringBuffer();
				taskemailString = arrayBuffer.append(taskemailString).append("@99bill.com").toString();
			}*/
			RnmItem svnpath2temppath = null;
			StringBuffer arrayBuffer2 = null;
			StringBuffer arrayBuffer5 = null;
			StringBuffer arrayBuffer6 = null;
			for (RnmItem rnmItem : rnmItemMap.getValue()) {
				svnpath2temppath = new RnmItem();
				svnpath2temppath.setAuthor(taskemailString);
				svnpath2temppath.setSonarjobName(sonarjobname);
				svnpath2temppath.setBuildPath(buildpath);
				svnpath2temppath.setDealbuildxmlPath(dealbuildxmlpath);
				svnpath2temppath.setReleaseId(rnmItem.getReleaseId());

				if (!rnmItem.getFilePath().contains("/src/test/java")) {
					//					Pattern pattern = Pattern.compile("(?<=^((U\\s)|(A\\s)|(\\wU)|(A\\+))\\s{2})(.*)");
					//					System.out.println("rnmItem.getFilePath()＝" +rnmItem.getFilePath());
					Matcher mat = pattern.matcher(rnmItem.getFilePath());
					boolean result = mat.find();
					while (result) {
						arrayBuffer2 = new StringBuffer();
						svnpath2temppath.setSvnPath(arrayBuffer2.append("http://svn.99bill.net/opt/99billsrc/").append(mat.group()).toString());
						result = mat.find();

					}
					//					Pattern pattern2 = Pattern.compile("(?<=src/main/java/)(.*?)(?=/[a-zA-z0-9]+\\.java)");
					Matcher mat2 = pattern2.matcher(rnmItem.getFilePath());
					boolean result2 = mat2.find();
					while (result2) {
						arrayBuffer5 = new StringBuffer();
						svnpath2temppath.setTempfilePath(arrayBuffer5.append(buildpath).append("/src/").append(mat2.group()).toString());
						result2 = mat2.find();
					}
					Matcher mat33 = pattern3.matcher(rnmItem.getFilePath());
					boolean result33 = mat33.find();
					while (result33) {
						arrayBuffer6 = new StringBuffer();
						svnpath2temppath.setTranEncodePath(arrayBuffer6.append(buildpath).append("/src/").append(mat33.group()).toString());
						result33 = mat33.find();
					}

					listcontent.add(svnpath2temppath);
				}
			}
			SonarMapNew.put(taskemailString, listcontent);
		}
		return SonarMapNew;
	}

}
